<template>
  <div></div>
</template>

<script>
import { getAppAuthTokenByCode, createAlipayApp } from "@/api/alipay/empower";
export default {
  name: "NamesIndex",

  data() {
    return {
      authorizeInfo: {},
      sysUserId: null,
    };
  },
  created() {
    this.getLocationlink();
  },
  methods: {
    getLocationlink() {
      var urlParams = new URLSearchParams(window.location.search);
      var app_auth_code = urlParams.get("app_auth_code");
      var app_id = urlParams.get("app_id");
      var sysUserId = urlParams.get("id");
      this.app_auth_code = app_auth_code;
      this.app_id = app_id; //第三方应用id
      this.sysUserId = sysUserId;
      console.log(app_auth_code); // 输出: "P0a26764e3b7d4a559479d38d496ec87"
      console.log(app_id); // 输出: "2021004134651039"
      if (app_auth_code) {
        this.getToken(app_auth_code);
      }
    },
    getToken(app_auth_code) {
      const that = this;
      const grantType = "authorization_code";
      const code = app_auth_code;

      getAppAuthTokenByCode(grantType, code).then((res) => {
        console.log("我来授权了", res);
        const authorizeInfo = res.data.alipay_open_auth_token_app_response;
        this.authorizeInfo = authorizeInfo;
        that.createApp();
      });
    },
    createApp() {
      const authTime = this.getDate();
      console.log("authTime", authTime);

      const reqBody = {
        appId: this.authorizeInfo.auth_app_id,
        thirdAppId: this.app_id,
        appName: "同锦起名",
        appLogo:
          "https://gw.alipayobjects.com/mdn/rms_d00b4e/afts/img/A*kgFUSaH_WzEAAAAAAAAAAAAAARQnAQ",
        authTime: authTime,
        appAuthToken: this.authorizeInfo.app_auth_token,
        authUserId: this.authorizeInfo.user_id,
        sysUserId: this.sysUserId, //系统租户id
        usageQuantity: 0,
      };
      createAlipayApp(reqBody).then((res) => {
        console.log("创建成功", res);
        this.$router.push("/distribution/alipay/index");
      });
    },

    getDate() {
      let time = new Date();
      console.log(time);
      //Fri Apr 15 2022 10:37:52 GMT+0800 (中国标准时间)
      var y = time.getFullYear();
      var m = (time.getMonth() + 1).toString().padStart(2, "0");
      //0-11月，0代表一月，所以获取当前月份要加1
      //用padStart方法，不足两位数，在前面添加0，以下同理
      var d = time.getDate().toString().padStart(2, "0");
      var hh = time.getHours().toString().padStart(2, "0");
      var mm = time.getMinutes().toString().padStart(2, "0");
      var ss = time.getSeconds().toString().padStart(2, "0");
      //yyyy-MM-dd HH:mm:ss
      return y + "-" + m + "-" + d + " " + hh + ":" + mm + ":" + ss;
    },
  },
};
</script>

<style></style>
